//
//  HMStatusCell.swift
//  CZBKWB31
//
//  Created by Linkon Y on 16/8/13.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import SnapKit
class HMStatusCell: UITableViewCell {
   //底部视图的顶部约束
    var bottomViewTopConstraint: Constraint?
    
    //创建ViewModel 供外界赋值
    var statusViewModel : HMStatusViewModel?{
        didSet{
            
            //            print(statusViewModel?.statusModel?.retweeted_status)
            
            //原创微博
            originalView.statusViewModel = statusViewModel
            
            //01 卸载约束!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
            bottomViewTopConstraint?.uninstall()
            //没有转发微博
            if statusViewModel?.statusModel?.retweeted_status == nil {
                bottomView.snp_updateConstraints(closure: { (make) in
                    bottomViewTopConstraint = make.top.equalTo(originalView.snp_bottom).constraint
                })
                //隐藏转发微博 cell复用问题
                retweetView.hidden = true
            }else{
                //有转发微博
                //赋值
                retweetView.statusViewModel = statusViewModel
                bottomView.snp_updateConstraints(closure: { (make) in
                    bottomViewTopConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
                })
                //显示转发微博
                retweetView.hidden = false
            }
            
            // 底部视图赋值
            bottomView.statusViewModel = statusViewModel
        }
    }
    

   override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
    setUpUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

     // MARK: - 设置视图
    private func setUpUI(){
       backgroundColor = RGB(237, green: 237, blue: 237)
       
        //增加控件
        contentView.addSubview(originalView)
        contentView.addSubview(retweetView)
        contentView.addSubview(bottomView)
        
        //增加约束
        //01
        originalView.snp_makeConstraints { (make) in
            make.leading.trailing.equalTo(contentView)
            //cell上增加条线
            make.top.equalTo(8)
            
            //该约束已经在自己的类约束了
//            make.height.equalTo(50)
        }
        //02
        retweetView.snp_makeConstraints { (make) in
            make.leading.trailing.equalTo(contentView)
            make.top.equalTo(originalView.snp_bottom)
             //该约束已经在自己的类约束了
//            make.height.equalTo(60)
        }
        //03
        bottomView.snp_makeConstraints { (make) in
            //处理约束冲突
           make.leading.trailing.equalTo(contentView)
           bottomViewTopConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
           make.height.equalTo(40)
        }

        contentView.snp_makeConstraints { (make) in
            make.leading.top.trailing.equalTo(self)
            make.bottom.equalTo(bottomView)
        }
   
    }
     // MARK: - 懒加载控件
    //原创微博
    private lazy var originalView : HMStatusOriginalView = HMStatusOriginalView()
    //转发微博
    private lazy var retweetView : HMStatusRetweetView = HMStatusRetweetView()
    //底部视图
    private lazy var bottomView : HMStatusBottomView = HMStatusBottomView()
  
}
 